package com.world.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger配置
 *
 * @author WangYX
 * @version 1.0.0
 * @date 2023/06/01 11:06
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {


    /**
     * 配置文档信息
     *
     * @param
     * @return {@link springfox.documentation.service.ApiInfo}
     * @author WangYX
     * @date 2023/07/27 11:05
     */
    private ApiInfo apiInfo() {
        Contact contact = new Contact("邓驴", null, "664160450@qq.com");
        return new ApiInfo("World接口文档", "World接口文档说明", "v1.0", null, contact, null, null, new ArrayList<>());
    }


    /**
     * 配置完成之后，在swagger页面就只能看到，配置的包下的所有接口。
     *
     * @author WangYX
     * @date 2023/07/27 11:16
     * @version 1.0.0
     */
    @Bean
    public Docket docket() {

        //添加header参数token
        ParameterBuilder builder = new ParameterBuilder();
        builder.name("Authorization")
                .description("令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        //添加header参数
        List<Parameter> pars = new ArrayList<Parameter>();
        pars.add(builder.build());


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //配置是否启用Swagger，如果是false，在浏览器将无法访问
                .enable(true)
                .select() // 通过.select()方法，去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors.basePackage("com.world"))
                .build()
                //添加header参数
                .globalOperationParameters(pars);
    }

}
